package com.theanh.RssActivity.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import com.theanh.RssActivity.Store;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;

public class FileUtils {
	/**
	 * Check exist file
	 */
	public static boolean isExisted(String filePath) {
		File store = null;

		try {
			store = new File(filePath);
			// delete if exists
			if (store.exists()) {
				store = null;
				return true;
			} else {
				store = null;
				return false;
			}
		} catch (Exception e) {
			return false;
		}
	}
	
	public static boolean deviceHasExternalCard() {
		return FileUtils.isExisted("/sdcard/");
	}
	
	public static void createDirectory(String filePath)
	{
		File store = null;

		try {
			store = new File(filePath);
			// delete if exists
			if (store.exists()) {
				store = null;
				return;
			} else {
				store.mkdir();
				store = null;
			}
		} catch (Exception e) {
		}
	}
	
	public static boolean isFileInExternalCard(String url) {
		if (url == null)
			return false;

		return url.indexOf("sdcard/") >= 0;
	}
	
	public static void writeImage(Bitmap bitmap, int id){
		if (bitmap == null)
			return;
		
		FileOutputStream fos;
		File store = null;
		String file = "";
		if (deviceHasExternalCard())
			file = Constants.EXTERNAL_IMAGES_DIRECTORY + id;
		else
			file = Constants.IMAGES_DIRECTORY + id;
		
		try {
			store = new File(file);
			// delete if exists
			if (!store.exists()) {
				// create file
				if (store.createNewFile()) {
					//fos = Store.context.openFileOutput(file, Context.MODE_WORLD_READABLE);
					fos = new FileOutputStream(store);
					bitmap.compress(CompressFormat.JPEG, Constants.IMAGE_QUANLITY, fos);
					fos.flush();
					fos.close();
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void writeImage(Bitmap bitmap, String name){
		if (bitmap == null)
			return;
		
		FileOutputStream fos;
		File store = null;
		String file = "";
		if (deviceHasExternalCard())
			file = Constants.EXTERNAL_IMAGES_DIRECTORY + name;
		else
			file = Constants.IMAGES_DIRECTORY + name;
		
		try {
			store = new File(file);
			// delete if exists
			if (!store.exists()) {
				// create file
				if (store.createNewFile()) {
					//fos = Store.context.openFileOutput(file, Context.MODE_WORLD_READABLE);
					fos = new FileOutputStream(store);
					bitmap.compress(CompressFormat.JPEG, Constants.IMAGE_QUANLITY, fos);
					fos.flush();
					fos.close();
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static Bitmap loadImage(int id){
		String file = "";
		BitmapDrawable bitmap = null;
		file = Constants.EXTERNAL_IMAGES_DIRECTORY + id;
		if (isExisted(file))
			bitmap = new BitmapDrawable(file);
		else {
			file = Constants.IMAGES_DIRECTORY + id;
			if (isExisted(file))
				bitmap = new BitmapDrawable(file);
		}
		
		return bitmap == null ? null : bitmap.getBitmap();
	}
	
	public static Bitmap loadImage(String name){
		String file = "";
		BitmapDrawable bitmap = null;
		file = Constants.EXTERNAL_IMAGES_DIRECTORY + name;
		if (isExisted(file))
			bitmap = new BitmapDrawable(file);
		else {
			file = Constants.IMAGES_DIRECTORY + name;
			if (isExisted(file))
				bitmap = new BitmapDrawable(file);
		}
		
		return bitmap == null ? null : bitmap.getBitmap();
	}
	
	/**
	 * Delete file
	 */
	public static void deleteFile(String filePath) throws Exception {
		File store = new File(filePath);
		// delete if exists
		if (store.exists()) {
			store.delete();
		}
	}
	
	public static void deleteImage(int id){
		try{
			String file = Constants.EXTERNAL_IMAGES_DIRECTORY + id;
			if (isExisted(file))
				deleteFile(file);
			else {
				file = Constants.IMAGES_DIRECTORY + id;
				deleteFile(file);
			}
		}catch(Exception ex){
			// Do nothing
		}
	}
	
	public static void deleteImage(String name){
		try{
			String file = Constants.EXTERNAL_IMAGES_DIRECTORY + name;
			if (isExisted(file))
				deleteFile(file);
			else {
				file = Constants.IMAGES_DIRECTORY + name;
				deleteFile(file);
			}
		}catch(Exception ex){
			// Do nothing
		}
	}
}
